package org.example.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.security.model.SysRole;
import org.example.system.dao.SysRoleDao;
import org.example.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author LiJunYi
 * @since 2020-05-25
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements ISysRoleService
{
    @Autowired
    SysRoleDao roleDao;

    /**
     * 获取角色许可通过用户id
     *
     * @param id id
     * @return {@link Set}<{@link String}>
     */
    @Override
    public Set<String> getRolePermissionByUserId(Integer id)
    {
        List<SysRole> perms = roleDao.getRolePermissionByUserId(id);
        Set<String> permsSet = new HashSet<>();
        for (SysRole perm : perms)
        {
            if (ObjectUtil.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }

    /**
     * 获取角色通过用户id
     *
     * @param id id
     * @return {@link List}<{@link SysRole}>
     */
    @Override
    public List<SysRole> getRolesByUserId(Integer id)
    {
        return roleDao.getRolePermissionByUserId(id);
    }
}
